---
title: "GOP with Rick Analysis"
output:
  html_document: default
  pdf_document: default
---
```{r}
library(foreign)
library(stargazer)
library(rtweet)
library(scales)
library(texreg)
library(performance)
library(see)
library(margins)
a=read.csv("objection_vote_data.csv")
names(a)
```

#Recoding
```{r}
#Create %Not White
a$Percent_Not_White=1-a$White_Percent 
#Years in Office
a$Years.in.Office=as.numeric(a$Years.in.Office)
#Binary Indicator for White/Not White
a$Not_White=NA
a$Not_White=ifelse(a$Race.Ethnicity=="White - Non-Hispanic",0,1)

#Trump Popularity
a$Trump_minus_Romney=a$Trump_2020 - a$Romney_2012
#Legislator Ideology
a$IdeoRHCX2All=as.numeric(a$IdeoRHCX2All)
#From AZ or PA
a$az_leg=NA
a$az_leg=ifelse(a$Abrv=="AZ",1,0)
a$pa_leg=NA
a$pa_leg=ifelse(a$Abrv=="PA",1,0)

#Inverse Impeachment Vote 
a$no_impeach=NA  
a$no_impeach=ifelse(a$Impeach==0,1,0)
#inverse Commission Vote
a$no_commision=NA
a$no_commision=ifelse(a$Jan_6_Commision==0,1,0)


#Rescaling 
a$Romney_2012_rescaled=rescale(a$Romney_2012) #district partisanship
a$Trump_2016_rescaled=rescale(a$Trump_2016)
a$Trump_2020_rescaled=rescale(a$Trump_2020)
a$DistrictIdeology_rescaled=rescale(a$DistrictIdeology) #district ideology alternative

a$IdeoRHCX2All_rescaled=rescale(a$IdeoRHCX2All) #legislator ideology
a$DW_Nominate_Dim1_116_rescaled=rescale(a$DW_Nominate_Dim1_116) #116 DW Nominate for robustness check
a$Vote_share_rescaled=rescale(a$Vote_share) #Member 2020 Vote Share
a$Trump_minus_Romney_rescaled=rescale(a$Trump_minus_Romney)#Trump Popularity v1
a$Years.in.Office_rescaled=rescale(a$Years.in.Office) #Years in Office
a$Percent_Not_White_rescaled=rescale(a$Percent_Not_White) #% Not White


#second data set with stefanik as party leader instead of liz cheney for Commision Vote
b=a
b$Party.Leader[b$Name=="Liz Cheney"]=0
b$Party.Leader[b$Name=="Elise Stefanik"]=1
##### subset for observations not affected by redistricting
a_subset= subset(a,Redistrict==0)
```


#Correlations of interest
```{r}
#Measures of District Partisanship
cor(a$Trump_2020,a$Trump_2016)
cor(a$Trump_2020,a$Romney_2012) 
cor(a$Trump_2016,a$Romney_2012)

#District Partisanship and District Ideology
cor(a$Trump_2020,a$DistrictIdeology_rescaled) 
cor(a$Trump_2016,a$DistrictIdeology_rescaled) 
cor(a$Romney_2012,a$DistrictIdeology_rescaled) 

#District Partisanship and Percent Not white
cor(a$Trump_2020,a$Percent_Not_White)
cor(a$Trump_2016,a$Percent_Not_White) 
cor(a$Romney_2012,a$Percent_Not_White) 

#District Ideology and %Not White
cor(a$DistrictIdeology_rescaled,a$Percent_Not_White)

#Vote Share and District Partisanship
cor(a$Trump_2020,a$Vote_share) 
cor(a$Trump_2016,a$Vote_share) 
cor(a$Romney_2012,a$Vote_share) 

#Comparing Legislator Ideology Estimates
cor(a$IdeoRHCX2All_rescaled,a$DW_Nominate_Dim1_117)
cor(a$IdeoRHCX2All_rescaled,a$DW_Nominate_Dim1_116, use = "complete.obs")

```

#Predicting AZ and PA Vote
```{r}
az_vote= glm(AZ.Vote  ~ Romney_2012_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

pa_vote= glm(PA.Vote  ~ Romney_2012_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

htmlreg(list(az_vote,pa_vote), file = "Table1_AZ_PA.doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("AZ Objection", "PA Objection"),
custom.coef.names = c("Constant","District Partisanship","Member 2020 Vote Share","Trump Popularity", "Legislator Ideology","HFC Member","Party Leader","Tenure","Not White","Woman", "% District Not White","AZ Legislator","PA Legislator"))
```
Calculate R-Squared
```{r}
library(pscl)
pR2(az_vote) ["McFadden"]
pR2(pa_vote) ["McFadden"]
```

#Predicting Impeachment and Commision Vote
```{r}
no_impeachment= glm(no_impeach  ~ Romney_2012_rescaled + Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

no_commision= glm(no_commision  ~ Romney_2012_rescaled + Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled, family = binomial(link = "logit"), data = b, na.action = "na.omit") 

htmlreg(list(no_impeachment,no_commision), file = "Table2_impeach_commision.doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("No Impeachment", "No Commision"),
custom.coef.names = c("Constant","District Partisanship","Member 2020 Vote Share","Trump Popularity", "Legislator Ideology","Party Leader","Tenure","Not White","Woman", "% District Not White"))
```

Calculate R-Squared
```{r}
library(pscl)
pR2(no_impeachment) ["McFadden"]
pR2(no_commision) ["McFadden"]
```

#Alternative PA and AZ Models

Predicting AZ and PA Without Trump Populairty
```{r}
az_vote_Romney= glm(AZ.Vote  ~ Romney_2012_rescaled +Vote_share_rescaled  +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

pa_vote_Romney= glm(PA.Vote  ~ Romney_2012_rescaled +Vote_share_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit")

az_vote_Trump2016= glm(AZ.Vote  ~ Trump_2016_rescaled +Vote_share_rescaled  +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

pa_vote_Trump2016= glm(PA.Vote  ~ Trump_2016_rescaled +Vote_share_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

az_vote_Trump2020= glm(AZ.Vote  ~ Trump_2020_rescaled +Vote_share_rescaled  +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

pa_vote_Trump2020= glm(PA.Vote  ~ Trump_2020_rescaled +Vote_share_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

htmlreg(list(az_vote_Romney,pa_vote_Romney,az_vote_Trump2016,pa_vote_Trump2016, az_vote_Trump2020,  pa_vote_Trump2020), file = "Appendix_Comparing_Disrict_Partisanship_Variables.doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("AZ Objection","PA Objection","AZ Objection", "PA Objection", "AZ Objection", "PA Objection"),
custom.coef.names = c("Constant","Romney 2012 Vote Share","Member 2020 Vote Share","Legislator Ideology","HFC Member","Party Leader","Tenure","Not White","Woman", "% District Not White","AZ Legislator","PA Legislator","Trump 2016 Vote Share","Trump 2020 Vote Share"))
```

Omit Observations Affected by Redistricting
```{r}
az_vote= glm(AZ.Vote  ~ Romney_2012_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a_subset, na.action = "na.omit") 

pa_vote= glm(PA.Vote  ~ Romney_2012_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a_subset, na.action = "na.omit") 

htmlreg(list(az_vote,pa_vote), file = "Omit_Redistrict_Obs.doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("AZ Objection", "PA Objection"),
        custom.coef.names = c("Constant","District Partisanship","Member 2020 Vote Share","Trump Popularity", "Legislator Ideology","HFC Member","Party Leader","Tenure","Not White","Woman", "% District Not White","AZ Legislator"))
```

Omit HFC Membership
```{r}
az_vote= glm(AZ.Vote  ~ Romney_2012_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 


pa_vote= glm(PA.Vote  ~ Romney_2012_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

htmlreg(list(az_vote,pa_vote), file = "Without_HFC.doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("AZ Objection", "PA Objection"),
custom.coef.names = c("Constant","District Partisanship","Member 2020 Vote Share","Trump Popularity", "Legislator Ideology","Party Leader","Tenure","Not White","Woman", "% District Not White","AZ Legislator","PA Legislator"))
```

Use DW-Nominate from 116th, omitting freshmen (and without HFC)
```{r}
az_vote= glm(AZ.Vote  ~ Romney_2012_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +DW_Nominate_Dim1_116_rescaled + Party.Leader+Years.in.Office_rescaled+ Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 


pa_vote= glm(PA.Vote  ~ Romney_2012_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +DW_Nominate_Dim1_116_rescaled  +Party.Leader+Years.in.Office_rescaled+ Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

htmlreg(list(az_vote,pa_vote), file = "Use_DW_Nominate_116_Omit HFC.doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("AZ Objection", "PA Objection"),
custom.coef.names = c("Constant","District Partisanship","Member 2020 Vote Share","Trump Popularity", "Legislator Ideology (DW Nominate)","Party Leader","Tenure","Not White","Woman", "% District Not White","AZ Legislator","PA Legislator"))
```

Using Trump 2016 Instead of Romney 2012
```{r}
az_vote= glm(AZ.Vote  ~ Trump_2016_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 


pa_vote= glm(PA.Vote  ~ Trump_2016_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

htmlreg(list(az_vote,pa_vote), file = "Trump 2016.doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("AZ Objection", "PA Objection"),
custom.coef.names = c("Constant","District Partisanship","Member 2020 Vote Share","Trump Popularity", "Legislator Ideology","HFC Member","Party Leader","Tenure","Not White","Woman", "% District Not White","AZ Legislator","PA Legislator"))

```


Using Trump 2020 Instead of Romney 2012
```{r}
az_vote= glm(AZ.Vote  ~ Trump_2020_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 


pa_vote= glm(PA.Vote  ~ Trump_2020_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

htmlreg(list(az_vote,pa_vote), file = "Trump 2020.doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("AZ Objection", "PA Objection"),
custom.coef.names = c("Constant","District Partisanship","Member 2020 Vote Share","Trump Popularity", "Legislator Ideology","HFC Member","Party Leader","Tenure","Not White","Woman", "% District Not White","AZ Legislator","PA Legislator"))


```

Calculate R-Squared
```{r}
library(pscl)
pR2(az_vote) ["McFadden"]
pR2(pa_vote) ["McFadden"]
```


# District Ideology Variations
```{r}
az_vote= glm(AZ.Vote  ~ Romney_2012_rescaled+DistrictIdeology_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

pa_vote= glm(PA.Vote  ~ Romney_2012_rescaled+DistrictIdeology_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

htmlreg(list(az_vote,pa_vote), file = "District Ideology_And_Partisanship.doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("AZ Objection", "PA Objection"),
custom.coef.names = c("Constant","District Partisanship","District Ideology","Member 2020 Vote Share","Trump Popularity", "Legislator Ideology","HFC Member","Party Leader","Tenure","Not White","Woman", "% District Not White","AZ Legislator","PA Legislator"))
```

```{r}
az_vote= glm(AZ.Vote  ~ DistrictIdeology_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

pa_vote= glm(PA.Vote  ~ DistrictIdeology_rescaled +Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

htmlreg(list(az_vote,pa_vote), file = "District Ideology only.doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("AZ Objection", "PA Objection"),
custom.coef.names = c("Constant","District Ideology","Member 2020 Vote Share","Trump Popularity", "Legislator Ideology","HFC Member","Party Leader","Tenure","Not White","Woman", "% District Not White","AZ Legislator","PA Legislator"))
```

```{r}
az_vote= glm(AZ.Vote  ~ Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + az_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

pa_vote= glm(PA.Vote  ~ Vote_share_rescaled +Trump_minus_Romney_rescaled +IdeoRHCX2All_rescaled +HFC +Party.Leader+Years.in.Office_rescaled+Not_White+Female+Percent_Not_White_rescaled + pa_leg, family = binomial(link = "logit"), data = a, na.action = "na.omit") 

htmlreg(list(az_vote,pa_vote), file = "Neither .doc",
        stars = c(.01,.05,.1),
        custom.model.names = c("AZ Objection", "PA Objection"),
custom.coef.names = c("Constant","Member 2020 Vote Share","Trump Popularity", "Legislator Ideology","HFC Member","Party Leader","Tenure","Not White","Woman", "% District Not White","AZ Legislator","PA Legislator"))
```

